package com.lyw.i02sync;

/**
 * 买票
 *
 * 第一步 创建资源类， 定义属性和方法
 *
 * @author lyw
 * @Date 2023/7/9-上午12:10
 */
class Ticket {

  // 票数
  private Integer number = 30;

  // 卖票
  public synchronized void sale() {
    if (number > 0) {
      number--;
      System.out.println(Thread.currentThread().getName() + "线程的票卖出去了，还剩 " + number + "张！");
    }
  }
}
public class SaleTicket {

  public static void main(String[] args) {

    for (int i = 0; i < 1; i++) {
      new Thread(() -> {
        Ticket ticket = new Ticket();
        for (int j = 0; j < 40; j++) {
          ticket.sale();
        }
        ticket.sale();
      }, "线程" + i).start();
    }
  }
}
